package com.adee.gulimall.ware.service.impl;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.common.utils.Query;
import com.adee.gulimall.ware.dao.PurchaseDetailDao;
import com.adee.gulimall.ware.entity.PurchaseDetailEntity;
import com.adee.gulimall.ware.service.PurchaseDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        String key = (String)params.get("key");
//        if(!StringUtils.isEmpty(key)) {
//            wrapper.eq("id", key)
//                    .or().like("name", key)
//                    .or().like("address", key)
//                    .or().like("areacode", key);
//        }
        try {
            Integer status = Integer.parseInt((String)params.get("status"));
            if(status != null) {
                wrapper.eq("status", status);
            }
        } catch (Exception e) {
            log.error("status转换异常：{}", params.get("status"), e);
        }

        try {
            Long wareId = Long.parseLong((String)params.get("wareId"));
            if(wareId != null && wareId > 0) {
                wrapper.eq("ware_id", wareId);
            }
        } catch (Exception e) {
            log.error("wareId转换异常：{}", params.get("wareId"), e);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

}